package com.tuanzi.loan.web.bean.settlement;

import com.google.common.collect.Lists;
import com.starbaba.model.utils.DateUtil;
import com.tuanzi.loan.business.constants.settlement.CompanyCooperationMode;
import com.tuanzi.loan.business.entity.settlement.CompanyInfo;
import com.tuanzi.loan.business.entity.settlement.SettlementPriceChangeRecord;
import com.tuanzi.loan.business.service.settlement.CompanyInfoService;
import com.tuanzi.loan.business.service.settlement.SettlementPriceChangeRecordService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;
import static com.tuanzi.loan.business.constants.DictionaryCode.SETTLEMENT_BILLING_CONTENT;
import static com.tuanzi.loan.business.constants.DictionaryCode.SETTLEMENT_RECEIVE_ACCOUNT;

/**
 * @author caiwanlin
 * @create 2018-01-16 19:26
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class SettlementPriceInfoBean extends JSFInfoBean<SettlementPriceChangeRecord> {
    private static final long serialVersionUID = -8575747247228776013L;

    @Autowired
    private SettlementPriceChangeRecordService settlementPriceChangeRecordService;

    @Autowired
    private CompanyInfoService companyInfoService;

    private List<CompanyInfo> companyInfoList;

    private String minDate;

    @PostConstruct
    private void init() {
        //生效时间设置
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        this.minDate = DateUtil.getDateString(calendar.getTime(), "yyyy-MM-dd");
        String id = getParamFromRequest("id");
        if (StringUtils.isNotBlank(id)) {
            $ = settlementPriceChangeRecordService.findOne(Long.parseLong(id));
        } else {
            $ = new SettlementPriceChangeRecord();
            changeStatusToAdd();
        }
        //查找所有公司名称
        companyInfoList = companyInfoService.findAllCompanyName();
    }


    /**
     * 查询
     */
    public void onFind() {
        settlementPriceChangeRecordService.findCompanyInfo($);
        if ($.getCompanyId() == null) {
            addWarnMessage("没有查找的产品");
        }
    }

    /**
     * 保存
     */
    public void onSave() {
        Integer copMode = $.getCopMode();
        Integer newCopMode = $.getNewCopMode();
        Float newYuanPrice = $.getNewYuanPrice();
        Float oldYuanPrice = $.getOldYuanPrice();
        Float oldPercentPrice = $.getOldPercentPrice();
        Float newPercentPrice = $.getNewPercentPrice();
        if (newCopMode != null && copMode != newCopMode){
            copMode = newCopMode;
        }
        if (copMode == CompanyCooperationMode.CPA.getIndex()){
            if (newYuanPrice == null && oldYuanPrice==null){
                addWarnMessage("【单位为元】的新结算价格不能为空");
                return;
            }
        }
        else if (copMode == CompanyCooperationMode.CPS.getIndex()){
            if (newPercentPrice == null && oldPercentPrice==null){
                addWarnMessage("【单位为%】的新结算价格不能为空");
                return;
            }
        }
        else if (copMode == CompanyCooperationMode.CPC.getIndex()){
            if (newYuanPrice == null && oldYuanPrice==null){
                addWarnMessage("【单位为元】的新结算价格不能为空");
                return;
            }
        }
        else if (copMode == CompanyCooperationMode.CPA_CPS.getIndex()){
            if (newYuanPrice == null && oldYuanPrice==null){
                addWarnMessage("【单位为元】的新结算价格不能为空");
                return;
            }
            if (newPercentPrice == null && oldPercentPrice==null){
                addWarnMessage("【单位为%】的新结算价格不能为空");
                return;
            }
        }
        $ = settlementPriceChangeRecordService.saveChangeRecord($);

        addSuccessfullyMessage();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(COOPERATION_MODE);
    }

    /**
     * 改变新合作模式
     */
    public void onChangeCopMode(){
        System.out.println();
    }
}
